/*
 * Created by SharpDevelop.
 * User: David
 * Date: 2006-12-03
 * Time: 05:48
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.Diagnostics;

using tools;

using Labb4.Pooman.Players;

namespace Labb4.Entity
{
	/// <summary>
	/// This class manages all controlers. Controlers are typically classes derived from
	/// the super class Control. All controlers are added to this manager when created,
	/// and each frame the methods "UpdateAI" and "UpdateEntity" is called with the
	/// time since last update in ms as argument.
	/// 
	/// This class is a manager, and needs to be initialized with a call to "Initialize"
	/// before the method Update can be successfully called.
	/// 
	/// This class is also implemented as a singleton.
	/// </summary>
	public sealed class ControlManager : Manager
	{
		#region members
		
		/// <summary>
		/// The one instance of this class
		/// </summary>
		private static ControlManager		m_Instance = new ControlManager();
		
		/// <summary>
		/// References to all the known controlers
		/// </summary>
		private List<Control>				m_Controls;
		/// <summary>
		/// Reference to the control for player 1
		/// </summary>
		private PoomanControl				m_Player1Control;
		
		#endregion
		
		#region constructors
		
		/// <summary>
		/// Constructor of ControlManager, initializes members.
		/// </summary>
		private ControlManager() :
			base()
		{
			m_Controls = new List<Control>();
			m_Player1Control = null;
		}
		
		#endregion
		
		#region methods
		
		/// <summary>
		/// Adds a reference to a control. Controlers of type PoomanControl is not added.
		/// </summary>
		/// <param name="control"></param>
		public void AddControl(Control control)
		{
			if(!(control is PoomanControl))
			{
				m_Controls.Add(control);
			}
			else
			{
				Debug.WriteLine("Control not added");
			}
		}
		
		/// <summary>
		/// Gives a reference to Player1
		/// </summary>
		/// <param name="control">An object of PoomanControl</param>
		public void SetPlayer1Control(PoomanControl control)
		{
			m_Player1Control = control;
		}
		
		/// <summary>
		/// Removes all references to controlers from the manager.
		/// </summary>
		public void Clear()
		{
			m_Controls.Clear();
		}
		
		/// <summary>
		/// Removes the reference to one specific control.
		/// </summary>
		/// <param name="control"></param>
		public void RemoveControl(Control control)
		{
			if(m_Controls.Contains(control))
			{
				m_Controls.Remove(control);
			}
		}
		
		#endregion
		
		#region overrides
		
		/// <summary>
		/// The update routine, should be called once every frame through a call to the
		/// inherited method "Update".
		/// </summary>
		/// <param name="deltaTime">The time passed since the last call. Unit is (ms)</param>
		protected override void DoUpdate(ref int deltaTime)
		{
			if(m_Player1Control != null)
			{
				m_Player1Control.UpdateEntity(ref deltaTime);
			}
			
			foreach(Control it in m_Controls)
			{
				it.UpdateAI(ref deltaTime);
				it.UpdateEntity(ref deltaTime);
			}
		}
		
		#endregion
		
		#region properties
		
		/// <summary>
		/// Gets the one instance of this class.
		/// </summary>
		public static ControlManager Instance
		{
			get
			{
				return m_Instance;
			}
		}
		
		#endregion
	}
}
